package com.bianmaba.oauth2.client;

import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 测试时需要启动Authorization-server及resource-server
 */

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {WebApplication.class})
@AutoConfigureMockMvc
public class HttpClientOauth2Tester {

    @Autowired
    private MockMvc mvc;
    private RestTemplate template = new RestTemplate();

    @Test(expected = HttpClientErrorException.class)
    public void user8080Fail() {
        Map<String, String> data = template.getForObject("http://192.168.0.65:8080/user/1", HashMap.class);
        Assert.assertNotNull(data);
    }

    @Test(expected = HttpClientErrorException.class)
    public void order8082Fail() {
        Map<String, String> data = template.getForObject("http://192.168.0.65:8082/order/1", HashMap.class);
        Assert.assertNotNull(data);
    }

    @Test
    public void userOK() throws Exception {
        String pk = UUID.randomUUID().toString();

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/user/1");

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.is("1")));
    }

    @Test
    public void orderOK() throws Exception {
        String pk = UUID.randomUUID().toString();

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/order/1");

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.is("1")));
    }
}
